<?php

namespace App\Http\Controllers\Managea;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repositories\AdminuserRepository;
use App\Repositories\OrderRepository;
use App\Repositories\UsersRepository;
use App\Models\Adminuser;
use App\Traits\BaseFun;
use Illuminate\Support\Carbon;

class IndexController extends Controller
{
	public function __construct(AdminuserRepository $AdminuserRepository,OrderRepository $OrderRepository,UsersRepository $UsersRepository)
    {
        $this->AdminuserRepository=$AdminuserRepository;
        $this->OrderRepository=$OrderRepository;
        $this->UsersRepository=$UsersRepository;

    }
    public function index(Request $request)
    {   
        $param=[];
        $param['start_time'] = Carbon::now()->startOfDay();
        $param['end_time'] = Carbon::now()->endOfDay();
        $res['allOrder']=$this->OrderRepository->countzsa($param);//今日订单数
        $res['allPrice']=$this->OrderRepository->countzse($param);//今日订单金额
        $res['allYe']=$this->UsersRepository->countYe();//用户总余额
        $res['allUser']=$this->UsersRepository->countUser();//用户总数
        if ($res) {
         $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
      } else {
         $ArrDate = ['state' => 2, 'data' => '', 'msg' => '获取失败!'];
      }
      return BaseFun::jsonReturn($ArrDate);  
   }
   public function orderStatistics(Request $request,$type)
   {
        $param=[];
        if($type==1){
            $time=date('Y-m-d ',strtotime('-7 day')).'00:00:01';
        }elseif ($type==2) {
            $time=date('Y-m-d ',strtotime('-15 day')).'00:00:01';
        }elseif ($type==3) {
            $time=date('Y-m-d ',strtotime('-1 month')).'00:00:01';
        }
        $param['start_time'] = date('Y-m-d H:i:s',time());
        $param['end_time'] = $time;
        $res=$this->OrderRepository->orderStatistics($param);
       if ($res) {
         $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
      } else {
         $ArrDate = ['state' => 2, 'data' => '', 'msg' => '获取失败!'];
      }
      return BaseFun::jsonReturn($ArrDate);  
   }
   

  public function dologin(Request $request){
        $userInfo=Adminuser::where('username',$request->post('username'))->where('password',md5($request->post('password')))->first();
        $dataArr=[];
        if($userInfo['state']==1){
           $Token=md5(time().rand(1000,9999));
           $login_token=Adminuser::where('id',$userInfo['id'])->update(['login_token'=>$Token,'token_time'=>time()]);
            //return redirect()->route('index');
            $dataArr['token']=$Token;
            $dataArr['data']=$userInfo;
            $dataArr['state']=0;
            return BaseFun::jsonReturn($dataArr);
        }elseif($userInfo['state']==2){
            $dataArr['data']=$userInfo;
            $dataArr['state']=1;
            $dataArr['msg']='该帐号已被禁用!';
            // return redirect()->route('login');
            return BaseFun::jsonReturn($dataArr);    

        }else{
            $dataArr['data']=$userInfo;
            $dataArr['state']=1;
            $dataArr['msg']='帐号或密码错误!';
            // return redirect()->route('login');
            return BaseFun::jsonReturn($dataArr); 
        }

    }

}
